// pages/contract/my.js
const app = getApp();
const {
  formatDate,
  formatDateNoHasYear
} = require("../../utils/util");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchboxShow: false, // 弹出层显示与否，用于处理滚动穿透
    minDate: new Date(2022, 0, 1).getTime(),
    show: false,
    requestUrl: app.api.contractMy,
    requestParams: {
      isMy: true
    },
    defaultRequestParams: {
      isMy: true
    },
    searchForm: {
      part_a: null,
      contract_num: null,
      contract_begin_date: null,
      contract_end_date: null
    },

    fabs: [{
      iconPath: 'https://oa-meilai.obs.cn-jssz1.ctyun.cn/20221102145321081195.png',
      text: '搜索'
    }, {
      iconPath: 'https://oa-meilai.obs.cn-jssz1.ctyun.cn/20221102145517219958.png',
      text: '刷新'
    }],
  },

  showSearchBox() {
    this.setData({
      searchboxShow: true
    })
  },

  closeSearchBox() {
    this.setData({
      searchboxShow: false
    })
  },


  toChange({
    detail: {
      id
    }
  }) {
    wx.navigateTo({
      url: '/pages/contract/submit?origin_id=' + id
    });
  },

  toDetail({
    detail: {
      id
    }
  }) {
    wx.navigateTo({
      url: '/pages/contract/detail?id=' + id
    });
  },

  clickFab({
    detail
  }) {
    const _this = this
    if (detail === '刷新') {
      _this.resetSearch();
      _this.setData({
        requestParams:  Object.assign({}, _this.data.defaultRequestParams)
      }, function () {
        _this.selectComponent('.contract-list-component').handlePullDownRefresh()
      })
    }
    if (detail === '搜索') {
      _this.selectComponent('.search-box').show();
    }
  },

  // /**
  //  * 页面相关事件处理函数--监听用户下拉动作
  //  */
  // onPullDownRefresh() {
  //     this.selectComponent('.contract-list-component').handlePullDownRefresh()
  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.selectComponent('.contract-list-component').handleReachBottom()
  },
  // showSearchBox() {
  //     this.selectComponent('.search-box').show()
  // },
  inputChange(e) {
    this.setData({
      [e.target.dataset.key]: e.detail
    })
  },
  showCalendar() {
    this.setData({
      show: true
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  onConfirm(e) {
    this.setData({
      'searchForm.contract_begin_date': formatDate(e.detail[0]),
      'searchForm.contract_end_date': formatDate(e.detail[1]),
      show: false
    })
  },


  resetSearch() {
    this.setData({
      searchForm: {
        part_a: null,
        contract_num: null,
        contract_begin_date: null,
        contract_end_date: null
      }
    });
  },
  onSearch() {
    const _this = this;
    const _params = JSON.parse(JSON.stringify(_this.data.searchForm || {}));
      let params = {};
      if (!!_params) {
        const keys = Object.keys(_params) || [];
        keys.forEach(key => {
          if (_params[key] != null && _params[key] != undefined) params[key] = _params[key];
        });
      }
      _this.setData({
        requestParams: Object.assign({}, _this.data.defaultRequestParams, params)
      }, function () {
        _this.selectComponent('.contract-list-component').handlePullDownRefresh()
        _this.selectComponent('.search-box').close();
      })
  },
  onReset(){
    const _this = this;
    _this.resetSearch();
    _this.setData({
      requestParams:  Object.assign({}, _this.data.defaultRequestParams)
    }, function () {
      _this.selectComponent('.contract-list-component').handlePullDownRefresh()
      _this.selectComponent('.search-box').close();
    })
  },
})